<script setup lang="ts">
import * as Api from '@/api/xsfp.js'
import type { TableColumnCtx } from 'element-plus'
import {ref,onMounted} from "vue";
//分页管理
const tableData =ref([])
let total = ref(0)
let pageSize = ref(25)
let currentPage = ref(1)
let id = ref("")
let teachId = ref("")
const getData =async ()=> {
  let result = await Api.page({
    "currentPage": currentPage.value,
    "pageSize": pageSize.value,
    "id": id.value,
    "teachId": teachId.value,
  })
  if (result.code === 200){
    tableData.value = result.data.rows
    total.value = result.data.total
  }
}
const handleCurrentChange = (e) => {
  currentPage.value = e
  getData()
}
const handleSizeChange = (e)=>{
  pageSize.value = e
  getData()
}
onMounted(async () => {
  await getData()
  let result = await Api.list()
  options.value =result.data
})
const shuaxin =async ()=> {
  id.value='';
  teachId.value='';
  let result = await Api.page({
    "id":'',
    "teachId":'',
  })
  if (result.code === 200){
    tableData.value = result.data.rows
  }

}
//新增班级
let isAdd = ref(true)
const dialogVisible = ref(false)
import {ElMessage, ElMessageBox} from 'element-plus'
const formInline = ref({
  id:'',
  name:'',
  stuId: '',
  teachId: '',
  peopleSum: '',
})
const options = ref([])
const propst = {
  label:"name",
  value:"id"
}
const btuAdd = async ()=> {
  formInline.value.id = ""
  formInline.value.name = ""
  // formInline.value.stuId = ""
  formInline.value.teachId = ""
  formInline.value.peopleSum = ""
  isAdd.value = true
  dialogVisible.value=true
  let result = await Api.list()
  options.value =result.data
}
const addclass = async () => {
  let result = await Api.add({
    "id" : formInline.value.id,
    "name": formInline.value.name,
    "teachId":formInline.value.teachId,
    "peopleSum":formInline.value.peopleSum
  })
  if(result.code === 200){
    getData()
    dialogVisible.value=false
    ElMessage.success(result.message)
  }else {
    ElMessage.error(result.message)
  }
}
//修改
const updateclass = async () => {
  let result =await Api.update(formInline.value.id,{
    "id" : formInline.value.id,
    "name": formInline.value.name,
    // "stuId":formInline.value.stuId,
    "teachId":formInline.value.teachId,
    "peopleSum":formInline.value.peopleSum
  })
  if (result.code === 200){
    getData()
    dialogVisible.value=false
    ElMessage.success(result.message)
  }else {
    ElMessage.error(result.message)
  }
}

const btuUpdate =async (data) => {
  let result = await Api.list()
  options.value =result.data

  isAdd.value = false

  dialogVisible.value=true
  formInline.value.id =data.id
  formInline.value.name =data.name
  // formInline.value.stuId =data.stuId
  formInline.value.teachId =data.teachId
  formInline.value.peopleSum =data.peopleSum

}
const submit = ()=> {
  if (isAdd.value){
    addclass()
  }else {
    updateclass()
  }

}
//删除
const deleclass = async  (id) => {
  console.log(id);
  await ElMessageBox.confirm('您确定要删除吗','温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消',
  }).then(async ()=> {
    let result =  await Api.dele(id)
    if(result.code === 200) {
      getData()
      ElMessage.success(result.message)
    }else {
      ElMessage.error(result.message)
    }
  })
}

</script>

<template>
  <div style="position: absolute;top: 100px;width: 90%;height: 40px;border-bottom: solid 1px rgb(221.7, 222.6, 224.4);margin: auto">
    <el-row>
      <el-col :span="8">
                <el-button style="border: solid 1px #4285F4;border-radius: 20px;width: 150px;color: #4285F4;background-color:rgb(216.8, 235.6, 255);" @click="btuAdd()" >
                  新增班级<el-icon class="el-icon--right"><el-icon><Plus /></el-icon></el-icon>
                </el-button>
        <div class="grid-content ep-bg-purple" />
      </el-col>
      <el-col :span="6">
        <div class="grid-content ep-bg-purple-light" />
      </el-col>
      <el-col :span="10" >
        <div style="display:flex;align-items: center">
          <el-input
              v-model="id"
              style="width: 200px;padding-left: 5px;border-radius: 20px ;margin-right: 5px;margin-bottom: 18px"
              placeholder="请选择班级"
          />
          <el-select-v2
              v-model="teachId"
              filterable
              :options="options"
              :props="propst"
              placeholder="任教老师"
              style="width: 240px;margin-right: 20px;margin-bottom: 18px"
          />
          <el-button style="
          border: solid 1px #4285F4;
          border-radius: 20px;
          width: 20px;color: #4285F4;
          background-color:rgb(216.8, 235.6, 255);
          margin-bottom: 20px"
                     @click="getData">
            <el-icon ><el-icon><Search /></el-icon></el-icon>
          </el-button>

          <el-button style="
          border: solid 1px #4285F4;
          border-radius: 20px;
          width: 20px;
          color: #4285F4;
          background-color:rgb(216.8, 235.6, 255);
          margin-bottom: 20px"
                     @click="shuaxin">
            <el-icon ><el-icon><RefreshRight /></el-icon></el-icon>
          </el-button>
        </div>

        <div class="grid-content ep-bg-purple" />
      </el-col>
    </el-row>


  </div>
  <div style="height: 88%;width: 100%;margin-top: 80px">
    <el-table
        :data="tableData"
        :default-sort="{ prop: 'date', order: 'descending' }"
        style="width: 100%;height: 95%"
    >
      <el-table-column fixed type="index" label="编号" width="120" />
      <el-table-column prop="id" label="班级id" width="120" />
      <el-table-column prop="name" label="班级" width="180" />
      <el-table-column prop="peopleSum" label="人数" width="180" />
      <el-table-column prop="teachId" label="教师id" width="180" />
      <el-table-column fixed="right" label="操作" min-width="120">
        <template #default="scope">
          <el-button
              link
              size="small"
              style="color: #4285F4"
              @click="btuUpdate(scope.row)"
          >
            编辑
          </el-button>
          <el-button
              link
              size="small"
              style="color: #4285F4"
              @click="deleclass(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[25, 50, 100, 200]"

        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 10px"
    />
    <el-dialog
        v-model="dialogVisible"
        :title="isAdd ? '新增' : '修改' "
        width="1000"
    >
      <div style="width: 100% ; border-top: #6b778c 1px solid;margin-bottom: 50px"></div>
    <span>
     <el-form :inline="true" :model="formInline" class="demo-form-inline">
     <el-form-item label="班级id">
      <el-input v-model="formInline.id"  clearable />
    </el-form-item>
     <el-form-item label="班级名称">
      <el-input v-model="formInline.name"  clearable />
    </el-form-item>
<!--       <el-form-item label="关联学生id">-->
<!--      <el-input v-model="formInline.stuId"  clearable />-->
<!--    </el-form-item>-->
       <el-form-item label="班级人数">
      <el-input v-model="formInline.peopleSum"  clearable />
    </el-form-item>
       <el-form-item label="任教老师" >
         <el-select-v2
             v-model="formInline.teachId"
             filterable
             :options="options"
             :props="propst"
             placeholder="老师"
             style="width: 240px;margin-left: auto"
         />
    </el-form-item>
    </el-form>
    </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submit()">
            提交
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.el-pagination.is-background .btn-next.is-active {
  background-color: #fff !important;
}
input::placeholder {
  color: #FFFFFF;
}
</style>